package controller

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/combinedQuery/model"
	"gowork/combinedQuery/service"
)

type OrderController struct {
	Ctx  iris.Context
	Os *service.OrderService
}

func (oc *OrderController)PostQueryorderlist() mvc.Result{

	oc.Os = service.NewOrderService(model.NewOrder(), nil, nil)

	var qo model.QueryOrder
	err := oc.Ctx.ReadForm(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": "参数解析错误", "data":nil, "code":500,},
		}
	}

	fmt.Println(qo)

	orderList, totals , err := oc.Os.QueryOrderList(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": map[string]interface{}{"rows":nil, "totals":0}, "code":500,},
		}
	}

	fmt.Println(len(orderList))

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data":map[string]interface{}{"rows":orderList, "totals":totals}, "code":200,},
	}

}

func (oc *OrderController)PostQuerymeiyeorderlist() mvc.Result{

	oc.Os = service.NewOrderService(nil, model.NewMeiyeOrders(), nil)

	var qo model.MeiyeStatistical
	err := oc.Ctx.ReadForm(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": "参数解析错误", "data":nil, "code":500,},
		}
	}

	fmt.Println(qo)

	orderList, totals , err := oc.Os.Mmo.StatisticalQuery(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": map[string]interface{}{"rows":nil, "totals":0}, "code":500,},
		}
	}

	fmt.Println(len(orderList))

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data":map[string]interface{}{"rows":orderList, "totals":totals}, "code":200,},
	}

}

func (oc *OrderController)PostQuerymeiyeorderlistasync() mvc.Result{

	oc.Os = service.NewOrderService(nil, model.NewMeiyeOrders(), nil)

	var qo model.MeiyeStatistical
	err := oc.Ctx.ReadForm(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": "参数解析错误", "data":nil, "code":500,},
		}
	}

	fmt.Println(qo)

	orderList, totals , err := oc.Os.Mmo.StatisticalQueryAsync(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": map[string]interface{}{"rows":nil, "totals":0}, "code":500,},
		}
	}

	fmt.Println(len(orderList))

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data":map[string]interface{}{"rows":orderList, "totals":totals}, "code":200,},
	}

}

func (oc *OrderController)PostQuerycalllist() mvc.Result{

	oc.Os = service.NewOrderService(nil, nil, model.NewCallLog())

	var qo model.QueryTotalCall
	err := oc.Ctx.ReadForm(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": "参数解析错误", "data":nil, "code":500,},
		}
	}

	fmt.Println(qo)

	orderList, totals , err := oc.Os.Qtc.QueryCallLog(&qo)
	if err != nil {
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": map[string]interface{}{"rows":nil, "totals":0}, "code":500,},
		}
	}

	fmt.Println(len(orderList))

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data":map[string]interface{}{"rows":orderList, "totals":totals}, "code":200,},
	}

}